package cj.web.mall.dao;

import java.util.List;
import java.util.Map;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.ReturnGeneratedKeys;
import net.paoding.rose.jade.annotation.SQL;

@DAO
public interface UserDAO {

	@SQL("insert into SCBuyer(phone,deleteFlag,createUser,createAt,updateUser,updateAt) values(:1,1,:1,NOW(),:1,NOW())")
	void register(String phone);
	
	@SQL("select count(0) from SCBuyer where deleteFlag=1 and phone=:1")
	int isRegister(String phone);
	
	@SQL("select id from SCBuyer where deleteFlag=1 and phone=:1")
	int getBuyerId(String phone);
	
	@SQL("select phone from SCBuyer where id=:1")
	String getPhoneById(int id);

	@SQL("select id,receiverName,receiverPhone,province,city,area,address from SCAddress where buyerId=:1 and deleteFlag=1 and isDefault=1")
	List<Map> getDefaultAddress(int buyerId);

	@SQL("select id,receiverName,receiverPhone,isDefault,province,city,area,address from SCAddress where deleteFlag=1 and buyerId=:1 order by isDefault desc")
	List<Map> getAddrByBuyerId(int buyerId);

	@SQL("select id,buyerId,receiverName,receiverPhone,isDefault,province,city,area,address from SCAddress where id=:1")
	List<Map> getAddressById(String addressId);

	@ReturnGeneratedKeys
	@SQL("insert into SCAddress(buyerId,receiverName,receiverPhone,province,city,area,address,isDefault,deleteFlag,createAt,createUser,UpdateAt,UpdateUser) values(:1,:2,:3,:4,:5,:6,:7,:8,1,NOW(),:8,NOW(),:8)")
	int createAddress(int buyerId, String receiverName, String receiverPhone,
			String province, String city, String area, String address,
			int isDefault, String phone);

	@SQL("update SCAddress set receiverName=:2,receiverPhone=:3,province=:4,city=:5,area=:6,address=:7,isDefault=:8,updateAt=NOW() where id=:1")
	void updateAddress(String id, String receiverName, String receiverPhone,
			String province, String city, String area, String address,
			int isDefault);

	@SQL("update SCAddress set deleteFlag=0,updateAt=NOW() where id=:1")
	void delAddrById(int addressId);

	@SQL("update SCAddress set isDefault=0 where buyerId=:1 and isDefault=1 and deleteFlag=1")
	void notDefaultByBuyerId(int buyerId);

	@SQL("update SCAddress set isDefault=1 where id=:1")
	void setDefalut(String addressId);

	@SQL("select count(1) from SCAddress where deleteFlag=1 and buyerId=:1")
	int addrCntByBuyerId(int buyerId);

	@SQL("select count(1) from MemberDetail where deleteFlag=1 and userId=:1")
	int isExistMemberDetail(int userId);
	
	@SQL("select count(1) from MemberDetail where deleteFlag=1 AND type =:2 AND userId=:1")
	int isExistMemberDetailCmp(int userId,int type);
	
	@SQL("INSERT INTO MemberDetail ("
    		+ "userId,"
    		+ "type,"
    		+ "companyId,"
    		+ "name,"
    		+ "identityCard,"
    		+ "photo,"
    		+ "nickname,"
    		+ "sex,"
    		+ "isMarry,"
    		+ "weight,"
    		+ "height,"
    		+ "isOpen,"
    		+ "education,"
    		+ "school,"
    		+ "profession,"
    		+ "money,"
    		+ "roomOrCar,"
    		+ "isChildren,"
    		+ "infor,"
    		+ "selection,"
    		+ "province,"
    		+ "city,"
    		+ "area,"
    		+ "status,"
    		+ "deleteFlag,"
    		+ "createUser,"
    		+ "createAt,"
    		+ "updateUser,"
    		+ "updateAt,"
    		+ "companyName,"
    		+ "followCount"
			+") VALUES ("
			+ ":1,"
			+ ":2,"
			+ ":3,"
			+ ":4,"
			+ ":5,"
			+ ":6,"
			+ ":7,"
			+ ":8,"
			+ ":9,"
			+ ":10,"
			+ ":11,"
			+ ":12,"
			+ ":13,"
			+ ":14,"
			+ ":15,"
			+ ":16,"
			+ ":17,"
			+ ":18,"
			+ ":19,"
			+ ":20,"
			+ ":21,"
			+ ":22,"
			+ ":23,"
			+ "0,"
			+ "1,"
			+ ":24,"
			+ "now(),"
			+ ":24,"
			+ "now(),"
			+ ":25,"
			+ "0)")
	void create(int userId,int type,Integer companyId,String name,String identityCard,String photo,String nickname,
			int sex,int isMarry,String weight,String height,int isOpen,String education,String school,String profession,
			String money,String roomOrCar,int isChildren,String infor,String selection,String province,String city,
			String area,String phone,String companyName);
	
	@SQL("UPDATE MemberDetail SET photo=:1,nickname=:2,sex=:3,isMarry=:4,weight=:5,height=:6,isOpen=:7,education=:8,school=:9,"
			+ "profession=:10,money=:11,roomOrCar=:12,isChildren=:13,infor=:14,selection=:15,province=:16,city=:17,"
			+ "area=:18,updateUser=:19,updateAt=now() WHERE userId=:20")
	void update(String photo,String nickname,
			int sex,int isMarry,String weight,String height,int isOpen,String education,String school,String profession,
			String money,String roomOrCar,int isChildren,String infor,String selection,String province,String city,
			String area,String phone,int userId);
	
	@SQL("UPDATE MemberDetail SET photo=:1,nickname=:2,sex=:3,isMarry=:4,weight=:5,height=:6,isOpen=:7,education=:8,school=:9,"
			+ "profession=:10,money=:11,roomOrCar=:12,isChildren=:13,infor=:14,selection=:15,province=:16,city=:17,"
			+ "area=:18,updateUser=:19,updateAt=now(),companyName=:20 WHERE userId=:21")
	void update(String photo,String nickname,
			int sex,int isMarry,String weight,String height,int isOpen,String education,String school,String profession,
			String money,String roomOrCar,int isChildren,String infor,String selection,String province,String city,
			String area,String phone,String companyName,int userId);
	
	@SQL("SELECT a.*,b.name as companyNameGL,b.address as companyAddress,b.descs as companyDescs,c.phone as account FROM MemberDetail a left join Company b on a.companyId=b.id join SCBuyer c on a.userId=c.id WHERE a.deleteFlag=1 AND a.userId=:1")
	List<Map> getMemberDetailByUserId(int userId);
	
	@SQL("SELECT a.nickname,a.sex,a.userId,b.phone,a.name,a.infor,a.photo,a.isOpen,a.type,a.province,a.city,a.area,a.identityCard FROM MemberDetail a join SCBuyer b on a.userId=b.id WHERE a.deleteFlag=1 AND a.userId=:1")
	List<Map> getMemberDetailForUC(int userId);
	
	@SQL("select count(1) from PubActivityOrder where deleteFlag=1 and userId=:1")
	int getPubActivityOrderCount(int userId);
	
	@SQL("select count(1) from PubYH where deleteFlag=1 and userId=:1")
	int getPubYHCount(int userId);
	
	@SQL("select count(1) from CmpActivityOrder where deleteFlag=1 and userId=:1")
	int getCmpActivityOrderCount(int userId);
	
	@SQL("select count(1) from CmpYH where deleteFlag=1 and userId=:1")
	int getCmpYHCount(int userId);
	
	@SQL("select count(1) from Follow where userId=:1")
	int getFollowCount(int userId);
	
	@SQL("update MemberDetail set isOpen=:2 where userId=:1")
	void upMemberDetailById(int userId,int isOpen);
}